package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {
    private int scoreTeamA = 0;
    private int scoreTeamB = 0;
    private TextView scoreViewA;
    private TextView scoreViewB;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // 初始化视图
        scoreViewA = findViewById(R.id.team_a_score);
        scoreViewB = findViewById(R.id.team_b_score);

        // 绑定按钮点击事件
        setupButton(R.id.team_a_3_points, 3, true);
        setupButton(R.id.team_a_2_points, 2, true);
        setupButton(R.id.team_a_free_throw, 1, true);

        setupButton(R.id.team_b_3_points, 3, false);
        setupButton(R.id.team_b_2_points, 2, false);
        setupButton(R.id.team_b_free_throw, 1, false);

        // 重置按钮
        findViewById(R.id.reset_button).setOnClickListener(v -> {
            scoreTeamA = 0;
            scoreTeamB = 0;
            updateScore();
        });
    }

    // 通用按钮点击处理
    private void setupButton(int buttonId, int points, boolean isTeamA) {
        findViewById(buttonId).setOnClickListener(v -> {
            if (isTeamA) {
                scoreTeamA += points;
            } else {
                scoreTeamB += points;
            }
            updateScore();
        });
    }

    // 更新分数显示
    private void updateScore() {
        scoreViewA.setText(String.valueOf(scoreTeamA));
        scoreViewB.setText(String.valueOf(scoreTeamB));
    }
}